package com.draven.product.controller;

import com.draven.model.product.ProductDTO;
import com.draven.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ProductService productService;

    @RequestMapping("/getProduct/{id}")
    public String getProduct(@PathVariable("id") Long id) {
        return productService.getProduct(id);
    }

    @GetMapping("/updateStock/{id}/{num}")
    public void updateStock(@PathVariable("id") Long id, @PathVariable("num") Integer num) {
        try {
            productService.updateStock(id, num);
        }catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("库存不足");

        }
    }
    @RequestMapping("/page")
    List<ProductDTO> productPage(){
        return productService.productPage();
    }

    @RequestMapping("/detail/{id}")
    ProductDTO getProductDetail(@PathVariable("id") Long id) {
        return productService.getProductDetail(id);
    }



}
